package com.guiwu.gteway.industryapp.Regist;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.guiwu.gteway.industryapp.R;
import com.guiwu.gteway.industryapp.RootActivity;
import com.guiwu.gteway.industryapp.util.MyLog;
import com.guiwu.gteway.industryapp.util.PreferencesUtil;
import com.guiwu.gteway.industryapp.util.ProgressSimpleDialog;
import com.guiwu.gteway.industryapp.util.ToastUtil;
import com.guiwu.gteway.industryapp.widget.SimpleToolbar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.google.common.base.Preconditions.checkNotNull;

public class RegisterActivity extends RootActivity implements RegisterContract.View {
    private static final String TAG = "RegisterActivity";
    private RegisterContract.Presenter mPresenter;
    private Unbinder unbinder;

    @BindView(R.id.et_register_name)
    EditText mName;

    @BindView(R.id.et_register_company)
    EditText mCompany;

    @BindView(R.id.et_register_phone)
    EditText mPhoneNum;

    @BindView(R.id.register_et_register_verify_code)
    EditText mVerifyCode;

    @BindView(R.id.register_bt_register_send_verify)
    Button mSendVerifyCode;

    @BindView(R.id.bt_register_register)
    Button mRegisterBt;

    @BindView(R.id.et_password)
    EditText mPassWord;

    @BindView(R.id.et_password_repeat)
    EditText mPAsswordRepeat;

    @BindView(R.id.et_email)
    EditText mEmial;

    private String mReturnCode;
    private ProgressSimpleDialog dialog;

    public static Intent newIntent(Context context){
        Intent intent = new Intent(context,RegisterActivity.class);
        return intent;
    }

    private CountDownTimer timer = new CountDownTimer(60000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            if(!isFinishing()){
                //doing something...
                mSendVerifyCode.setText((millisUntilFinished / 1000) + "秒后可重发");
                mSendVerifyCode.setEnabled(false);
            }

        }

        @Override
        public void onFinish() {
            if (!isFinishing()){
                mSendVerifyCode.setText("获取验证码");
                mSendVerifyCode.setEnabled(true);

            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);
        unbinder = ButterKnife.bind(this);
        mPresenter = new RegisterPresenter(this);
        dialog = new ProgressSimpleDialog(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        if(timer!=null){
            timer.cancel();
            timer = null;
        }
        unbinder.unbind();
        super.onDestroy();
    }

    private void initView(){
        SimpleToolbar simpleToolbar = findViewById(R.id.register_simple_toolbar);
        simpleToolbar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.register_bt_register_send_verify)
    public void OnSendVerifyCode(){
        String phoneNum = mPhoneNum.getText().toString();
        if (checkPhoneInput(phoneNum)){
            timer.start();
            mPresenter.getVerificationCode(phoneNum);
        }
    }

    @OnClick(R.id.bt_register_register)
    public void OnRegister(){
        String phoneNum = mPhoneNum.getText().toString();
        String name = mName.getText().toString();
        String company = mCompany.getText().toString();
        String verifyCode = mVerifyCode.getText().toString();
        String password = mPassWord.getText().toString();
        String passwordRepeat = mPAsswordRepeat.getText().toString();
        String email = mEmial.getText().toString();
        if (checkRegisterInput(phoneNum,name,company,verifyCode,password,passwordRepeat)){
            mPresenter.askForRegister(name,company,phoneNum,password,email);
        }
    }


    private boolean checkRegisterInput(String phoneNum,String name, String company,String verifyCode,String password,String passwordRepeat){
        String error = null;
        boolean result = true;
        if (!phoneNum.matches("[1][358]\\d{9}")){
            error = "请输入正确手机号";
        }

        if (name.equals("")){
            error = "请补全用户名";
        }

        if (company.equals(""))
        {
            error = "请补全公司名";
        }

        if (!verifyCode.equals(mReturnCode)){
            error = "验证码错误";
        }

        if (password.equals("")){
            error = "请输入密码";
        }

        if (passwordRepeat.equals("")){
            error = "请再次输入密码";
        }

        if (!password.equals(passwordRepeat)){
            error = "密码不一致";
        }

        if (error != null){
            ToastUtil.showToast(this,error);
            result = false;
        }

        return result;
    }

    private boolean checkPhoneInput(@NonNull String phoneNum){
        String phoneNumMatch = "[1][358]\\d{9}";

        if (!phoneNum.matches(phoneNumMatch)){
            ToastUtil.showToast(this,"请输入正确手机号");
            return false;
        }
        return true;
    }

    @Override
    public void showLoadingIndicator(boolean activity) {
        if (activity && !isFinishing()){
            dialog.showPorgressDialog("正在注册");
        }else if (!activity && !isFinishing()){
            dialog.closeProgressDialog();
        }
    }

    @Override
    public void onGetVerifyCode(String verifyCode) {
        MyLog.i(TAG,verifyCode);
        mReturnCode = verifyCode;
    }

    @Override
    public void showGetVerifyCodeError(String errorMsg) {
        ToastUtil.showToast(this,errorMsg);
    }

    @Override
    public void OnRegister(boolean result, String msg) {
        if (result){
            ToastUtil.showToast(this,"注册成功");
            PreferencesUtil.getInstance(this).putString("phoneNum",mPhoneNum.getText().toString());
        }else {
            ToastUtil.showToast(this,"注册失败" + msg);
        }
    }

    @Override
    public void setPresenter(RegisterContract.Presenter presenter) {
        mPresenter = checkNotNull(presenter);
    }
}
